#include <modules\graphics\animatable.h>

namespace DelGE
{
	namespace Modules
	{
		namespace Graphics
		{
			Animatable::Animatable()
			{
				this->mAnimaitonMap = map<string, Animation*>();
				this->pCurrentAnimation = NULL;
			}

			Animatable::~Animatable()
			{
				this->pCurrentAnimation = NULL;

				this->DestroyAnimations();
			}

			void Animatable::AddAnimation(string sName, int iStartFrame, int iEndFrame, float fFPS)
			{
				Animation* pAnimation;
				if (this->mAnimaitonMap.find(sName) == this->mAnimaitonMap.end())
				{
					pAnimation = new Animation(sName, iStartFrame, iEndFrame, fFPS);
					this->mAnimaitonMap[sName] = pAnimation;
				}
			}

			void Animatable::RemoveAnimation(string sName)
			{
				map<string, Animation*>::iterator itor;

				itor = this->mAnimaitonMap.find(sName);
				if (itor != this->mAnimaitonMap.end())
				{
					delete this->mAnimaitonMap[sName];
					this->mAnimaitonMap.erase(itor);
				}
			}
			
			Animation* Animatable::GetAnimation(string sName)
			{
				return this->mAnimaitonMap[sName];
			}

			Animation* Animatable::GetAnimation(int iIndex)
			{
				int i = 0;
				map<string, Animation*>::iterator animationIt;
				for (animationIt = this->mAnimaitonMap.begin(); animationIt != this->mAnimaitonMap.end(); animationIt ++)
				{
					if (i == iIndex)
					{
						return (*animationIt).second;
					}
					i ++;
				}

				return NULL;
			}

			Animation* Animatable::GetCurrentAnimation()
			{
				return this->pCurrentAnimation;
			}

			map<string, Animation*>& Animatable::GetAnimationMap()
			{
				return this->mAnimaitonMap;
			}

			void Animatable::PlayAnimation(string sName, DelGEGraphics_Animation_Playstyle ePlaystyle)
			{
				if (this->mAnimaitonMap.find(sName) != this->mAnimaitonMap.end())
				{
					this->pCurrentAnimation = this->mAnimaitonMap[sName];
					this->pCurrentAnimation->SetPlaystyle(ePlaystyle);
					this->pCurrentAnimation->Start();
				}
			}
			
			void Animatable::StopAnimation()
			{
				if (this->pCurrentAnimation != NULL)
				{
					this->pCurrentAnimation->Stop();
					this->pCurrentAnimation = NULL;
				}
			}

			void Animatable::PauseAnimation()
			{
				if (this->pCurrentAnimation != NULL)
					this->pCurrentAnimation->Pause();
			}
			
			void Animatable::ResumeAnimation()
			{
				if (this->pCurrentAnimation != NULL)
					this->pCurrentAnimation->Resume();
			}
			
			bool Animatable::IsAnimationPlaying(string sName)
			{
				if (this->pCurrentAnimation != NULL)
					return (this->pCurrentAnimation->GetName() == sName);

				return false;
			}

			int Animatable::GetAnimationCount()
			{
				return this->mAnimaitonMap.size();
			}

			void Animatable::UpdateAnimation(float fDelta)
			{
				if (this->pCurrentAnimation != NULL)
					this->pCurrentAnimation->Update(fDelta);
			}

			void Animatable::DestroyAnimations()
			{
				if (!this->mAnimaitonMap.empty())
				{
					map<string, Animation*>::iterator itor;

					for (itor = this->mAnimaitonMap.begin(); itor != this->mAnimaitonMap.end(); ++itor)
						delete (Animation *)itor->second;

					this->mAnimaitonMap.clear();
				}
			}
		}
	}
}